package com.jmars.TS.service;

import com.jmars.TS.entity.*;
import com.jmars.TS.mapper.db1.SysJobLogMapper;
import com.jmars.TS.mapper.db1.WMSInOutMapper;
import com.jmars.TS.service.impl.NRIBServiceImpl;
import lombok.var;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

/**
 * @Author: whd
 * @DateTime: 2024/09/12 13:41
 * @msg:
 */
@Service
public class  BatchService {
    private static final Logger logger = LoggerFactory.getLogger(NRIBServiceImpl.class);
    //
    @Autowired
    SysJobLogMapper sysJobLogMapper;

    @Autowired
    WMSInOutMapper wmsInOutMapper;


    @Transactional
    public void batchInAsn(InAsnHeader item) {
        if(wmsInOutMapper.existInAsnHeader(item) > 0){
            wmsInOutMapper.updateInAsnHeader(item);
        }
        else{
            wmsInOutMapper.insertInAsnHeader(item);
            for (var detail:item.transInDtl ) {
                wmsInOutMapper.insertInAsnDetail(detail);
            }
        }
    }

    @Transactional
    public void batchInSo(InSoHeader item) {
        if (wmsInOutMapper.existInSoHeader(item) <= 0) {
            wmsInOutMapper.insertInSoHeader(item);
            if(item.getTransOutDtl() != null ){
                for (InSoDetail detail:item.getTransOutDtl() ) {
                    wmsInOutMapper.insertInSoDetail(detail);
                }
            }

        } else {
//            wmsInOutMapper.updateInSoHeader(item);
        }
    }

    @Transactional
    public void batchInOrderPickSts(List<InOrderPickSts> body,String invOrg,String consignor) {
        // 先情况表再插入数据
        for (InOrderPickSts item:body ) {

            // 只更新已完成的单子（1-完成 0-未完成）
            if(item.getPickSts() == 1){

                wmsInOutMapper.updateSoPushFlag3(item.getOrderId()); // 更新标志
                // 插入中间表
                item.setInvOrg(invOrg);
                item.setConsignor(consignor);
//                item.setOrderId(item.getOrderId());
//                item.setShopCode(item.getShopCode());

//                if(wmsInOutMapper.existInOrderPickSts(item) > 0){

//                }
//                else{
                    wmsInOutMapper.insertInOrderPickSts(item);
//                }

            }

        }

    }

    @Transactional
    public void batchInPickPrintLableInfo(List<InPickPrintLableInfo> body,String linecode) {
        logger.info("[inPickPrintLableInfoTask][拣货单状态获取]------>>batch批量处理开始");
        // 先情况表再插入数据
        for (InPickPrintLableInfo item:body ) {
            wmsInOutMapper.updateSoPushFlag2(item.getOrderId()); // 更新标志
            item.setLinecode(linecode);
            wmsInOutMapper.insertInPickPrintLableInfo(item); // 插入中间表
        }
        logger.info("[inPickPrintLableInfoTask][拣货单状态获取]------>>batch批量处理SQL结束");

        // 执行存储过程
        if(body.size()>0){
            HashMap<String, Object> params = new HashMap<>();
            wmsInOutMapper.call_SP_SQL_PICKING_LABLE(params);
            String spOut = (String) params.get("spOut"); // 从params中获取存储过程输出的结果
            //logger.info("[call_SP_SQL_PICKING_LABLE] 返回消息 sp_out = {}",spOut);

            //logger.info("[inPickPrintLableInfoTask][拣货单状态获取]------>>数量："+ body.size());
        }
        logger.info("[inPickPrintLableInfoTask][拣货单状态获取]------>>batch批量处理存储过程结束");
    }

    // 记录日志
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    public int jobLog(String jobName, String jobGroup, String jobTag, String jobMsg, String status, String exceptionInfo){
        try{
            return sysJobLogMapper.insert(new SysJobLog(jobName,jobGroup,jobTag,jobName + " " +jobMsg,status,exceptionInfo));
        }
        catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return 0;
        }
    }

}
